package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.core.domain.entity.SysUser;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.dto.AlertDataDto;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.mapper.ElderMapper;
import com.zzyl.nursing.service.IAlertDataService;
import com.zzyl.nursing.service.IElderService;
import com.zzyl.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 报警数据Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-05
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService {
    @Autowired
    private AlertDataMapper alertDataMapper;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 处理设备报警数据
     *
     * @param alertDataDto
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void handleAlerData(AlertDataDto alertDataDto) {
        AlertData alertData = getById(alertDataDto.getId());
        if (ObjectUtil.equals(alertData.getStatus(), 1)) {
            throw new RuntimeException("数据已被其他管理员处理");
        }
        // 获取当前数据一致的所有数据
        List<AlertData> list = list(Wrappers.<AlertData>lambdaQuery()
                .eq(AlertData::getIotId, alertData.getIotId())
                .eq(AlertData::getCreateTime, alertData.getCreateTime())
                .eq(AlertData::getFunctionId, alertData.getFunctionId())
                .eq(AlertData::getProductKey, alertData.getProductKey())
        );
        Long userId = UserThreadLocal.getUserId();
        SysUser user = sysUserService.selectUserById(userId);
        // 修改数据内容
        List<AlertData> newlist = list.stream().map(item -> {
            item.setProcessingTime(alertDataDto.getProcessingTime().toLocalDateTime());
            item.setProcessingResult(alertDataDto.getProcessingResult());
            item.setProcessorId(userId);
            item.setProcessorName(user.getNickName());
            item.setStatus(1);
            return item;
        }).collect(Collectors.toList());
        // 批量修改
        updateBatchById(newlist);
    }
    /**
     * 查询报警数据
     *
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id) {
        return  getById(id);
    }

    /**
     * 查询报警数据列表
     *
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData) {
        Long userId = UserThreadLocal.get();
        alertData.setUserId(userId);
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData) {
        return save(alertData) ? 1 : 0;
    }

    /**
     * 修改报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData) {
        return updateById(alertData) ? 1 : 0;
    }

    /**
     * 批量删除报警数据
     *
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除报警数据信息
     *
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id) {
        return removeById(id) ? 1 : 0;
    }

}
